<template>
  <div>
    <!-- 增加按钮 -->
    <el-button
      type="primary"
      size="default"
      style="margin: 10px 0"
      @click="showdiolog"
      >增加</el-button
    >
    <!-- 表格数据 -->
    <el-table style="width: 100%" border :data="list">
      <el-table-column type="index" label="序号" width="80px" align="center">
      </el-table-column>
      <el-table-column
        prop="tmName"
        label="品牌名称"
        width="width"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="prop"
        label="品牌logo"
        width="width"
        align="center"
      >
        <!-- //插槽 row $index-->
        <template slot-scope="{ row, $index }">
          <img :src="row.logoUrl" alt="" style="width: 100px; height: 100px" />
        </template>
      </el-table-column>
      <el-table-column prop="prop" label="操作" width="width" align="center">
        <template slot-scope="{ row, $index }">
          <!-- 插槽携带修改按钮和删除按钮 -->
          <el-button type="warning" size="mini" @click="updatanlog(row)"
            >修改</el-button
          >
          <el-button type="danger" size="mini" @click="deleteTradeMark(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 
          @current-change="handleCurrentChange(pager)" 当前页码数变化回调
          @size-change="handleSizeChange"  每页多少条数据
          @prev-click="handlePrevClick" 前一页
          @next-click="handleNextClick"   后一页 
          :total总数据条数 current-page : 当前页数 第8页
          page-size:每页多少条数据 page-sizes:可以选择每页多少条数据 layout:布局分页器的位置 page-count:连续页码 如果是9 则连续页码为7
        -->
    <!-- 分页器 -->
    <el-pagination
      :total="total"
      :current-page="1"
      :page-sizes="[3, 5, 8]"
      :page-size="3"
      layout=" prev, pager, next, jumper,->,total, sizes"
      style="margin-top: 20px; textalign: center"
      :page-count="3"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      @prev-click="handlePrevClick"
      @next-click="handleNextClick"
    >
    </el-pagination>
    <!-- 增加品牌 -->
    <el-dialog
      :title="!tmForm.id ? '增加品牌' : '修改品牌'"
      :visible.sync="dialogFormVisible"
    >
      <el-form style="width: 80%" :model="tmForm" :rules="rules"  ref="ruleForm">
        <el-form-item label="品牌名称" label-width="100px" prop="tmName">
          <el-input autocomplete="off" v-model="tmForm.tmName"></el-input>
        </el-form-item>
        <!-- 上传图片 无法使用v-model action设置上传地址 -->
        <el-form-item label="品牌logo" label-width="100px" prop="logoUrl">
          <el-upload
            class="avatar-uploader"
            action="dev-api/admin/product/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="tmForm.logoUrl" :src="tmForm.logoUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addorupdatamark">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "tradeMark",
  data() {
    return {
      page: 1, //代表分页器第几页
      limit: 3, //当前展示几条数据
      total: 0, //一共多少条数据
      list: [], //接收商品数据列表
      dialogFormVisible: false, //dialog显示与隐藏
      imageUrl: "",
      tmForm: {
        tmName: "",
        logoUrl: "",
      },
      rules: {
        tmName: [
          { required: true, message: "请输入品牌名称", trigger: "blur" },
          {
            min: 2,
            max: 8,
            message: "长度在 2 到 8 个字符",
            trigger: "blur",
          },
        ],
        logoUrl: [
          { required: true, message: "请选择品牌图片信息"},
        ],
      },
    };
  },
  methods: {
    async getPageList() {
      //解构参数:
      const { page, limit } = this;
      //这个api要带参数:
      let result = await this.$API.trademark.reqTradeMarkList(page, limit);
      if (result.code == 200) {
        this.total = result.data.total;
        this.list = result.data.records;
      }
    },
    //第几页请求
    handleCurrentChange(pager) {
      this.page = pager;
      this.getPageList();
    },
    //每页多少条数据请求
    handleSizeChange(size) {
      this.limit = size;
      this.getPageList();
    },
    //前一页发起请求
    handlePrevClick() {
      this.page--;
      this.getPageList();
    },
    //后一页发起请求
    handleNextClick() {
      this.page++;
      this.getPageList();
    },
    //点击增加按钮后弹框,并清除所有的数据
    showdiolog() {
      this.tmForm = {
        tmName: "",
        logoUrl: "",
      };
      this.dialogFormVisible = true;
    },
    updatanlog(row) {
      this.dialogFormVisible = true;
      this.tmForm = { ...row }; //浅拷贝 这里面的row含有id字段
      console.log(this.tmForm);
    },
    //图片上传成功回调
    handleAvatarSuccess(res, file) {
      this.tmForm.logoUrl = res.data;
    },
    //上传图片校验
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    //发起请求
    addorupdatamark() {
      this.$refs.ruleForm.validate(async (success) => {
        if (success) {
          this.dialogFormVisible = false;
          let result = await this.$API.trademark.addorupdataTradeMarkList(
            this.tmForm
          );
          if (result.code == 200) {
            this.$message({
              type: "success",
              message: this.tmForm.id ? "修改品牌成功" : "增加品牌成功",
            });
            this.handleCurrentChange(this.page);
          } 
        } else {
          console.log("增加失败");
          return false;
          
        }
      });
    },
    deleteTradeMark(row){
      this.$confirm(`此操作将永久删除编号${row.id}${row.tmName}, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          let result=await this.$API.trademark.reqDeleteTradeMark(row.id);
          if(result.code ===200){
             this.$message({
            type: 'success',
            message: '删除成功!'
          });
          console.log(this.list.length-1);
          if(this.list.length-1 ==0){
            this.handleCurrentChange(this.page-1);
          }else{
            this.handleCurrentChange(this.page) 
          }
          
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    }
  },
  mounted() {
    //请求接口数据:
    this.getPageList();
  },
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
